﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class ContractList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F9012;
        protected GridView gvContract;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ContractEntityList(ContractQueryEntity contractQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(contractQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(contractQueryEntity, "CONTRACT");
            base.BindData2GridView(this.gvContract, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvContract, "CONTRACT_ID");
                List<string> IsAduit = base.GetSelectedIDsInGridView(this.gvContract, "IS_AUDIT");
                if ((IsAduit != null) && (IsAduit.Count > 0))
                {
                    for (int i = 0; IsAduit.Count > i; i++)
                    {
                        if (IsAduit[i] == "Y")
                        {
                            this.lblMsg.Text = selectedID[i] + "单据已经审核,无法删除!";
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9110_011");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "CONTRACT") + base.GetGlobalMsgResource("F9110_009");
                    ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
                    this.BindData2ContractEntityList(contractQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_010") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvContract, "CONTRACT_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_011");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_012");
            }
            else
            {
                base.Response.Redirect(string.Format("Contract.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            contractQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = contractQueryEntity.CurrentPage;
            contractQueryEntity.CurrentPage = "0";
            this.gvContract.Columns[1].Visible = false;
            this.gvContract.AllowSorting = false;
            this.BindData2ContractEntityList(contractQueryEntity);
            this.gvContract.ShowFooter = false;
            this.hdnActionID.Value = "";
            contractQueryEntity.IsGetAll = false;
            contractQueryEntity.CurrentPage = currentPageIndex;
            this.SetContractQueryEntity(contractQueryEntity);
            base.Export2Excel("Contract.xls", this.gvContract);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            contractQueryEntity.CurrentPage = "0";
            this.BindData2ContractEntityList(contractQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            contractQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ContractEntityList(contractQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            contractQueryEntity.ContractID = "";
            contractQueryEntity.OwnerID = "";
            contractQueryEntity.ContractType = "";
            contractQueryEntity.StartTime = "";
            contractQueryEntity.EndTime = "";
            contractQueryEntity.IsActive = "";
            contractQueryEntity.IsAudit = "";
            this.BindData2ContractEntityList(contractQueryEntity);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("ContractQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            if (int.Parse(contractQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                contractQueryEntity.CurrentPage = (int.Parse(contractQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2ContractEntityList(contractQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
            if (int.Parse(contractQueryEntity.CurrentPage) > 0)
            {
                contractQueryEntity.CurrentPage = (int.Parse(contractQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2ContractEntityList(contractQueryEntity);
            }
        }

        private ContractQueryEntity GetContractQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ContractQueryEntity);
            }
            return (base.PageQueryEntitySession as ContractQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
                contractQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ContractEntityList(contractQueryEntity);
            }
        }

        protected void gvContract_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9012", this.gvContract);
        }

        protected void gvContract_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvContract_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvContract.DataKeys[e.NewSelectedIndex]["CONTRACT_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvContract_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ContractQueryEntity contractQueryEntity = this.GetContractQueryEntity();
                contractQueryEntity.SortField = e.SortExpression;
                if (contractQueryEntity.SortDirection == "ASC")
                {
                    contractQueryEntity.SortDirection = "DESC";
                }
                else if (contractQueryEntity.SortDirection == "DESC")
                {
                    contractQueryEntity.SortDirection = "ASC";
                }
                this.BindData2ContractEntityList(contractQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ContractQueryEntity contractQueryEntity = new ContractQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    contractQueryEntity.ContractID = this.PreviousPage.ContractID;
                    contractQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    contractQueryEntity.ContractType = this.PreviousPage.ContractType;
                    contractQueryEntity.StartTime = this.PreviousPage.StartTime;
                    contractQueryEntity.EndTime = this.PreviousPage.EndTime;
                    contractQueryEntity.IsActive = this.PreviousPage.IsActive;
                    contractQueryEntity.IsAudit = this.PreviousPage.IsAudit;
                }
                else
                {
                    contractQueryEntity.ContractID = "";
                    contractQueryEntity.OwnerID = "";
                    contractQueryEntity.ContractType = "";
                    contractQueryEntity.StartTime = "";
                    contractQueryEntity.EndTime = "";
                    contractQueryEntity.IsActive = "";
                    contractQueryEntity.IsAudit = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            contractQueryEntity.IsGetAll = false;
            contractQueryEntity.CurrentPage = "0";
            contractQueryEntity.PageSize = this.gvContract.PageSize.ToString();
            contractQueryEntity.SortField = "CT.ROWID";
            contractQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvContract.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvContract.Columns[1].Visible = false;
                this.gvContract.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetContractQueryEntity(contractQueryEntity);
            this.BindData2ContractEntityList(contractQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetContractQueryEntity(ContractQueryEntity contractQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = contractQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = contractQueryEntity;
            }
        }

        public ContractQuery PreviousPage
        {
            get
            {
                return (ContractQuery) base.PreviousPage;
            }
        }
    }
}

